/*     */ package com.zimbra.cs.stats;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.stats.Accumulator;
/*     */ import com.zimbra.common.stats.Counter;
/*     */ import com.zimbra.common.stats.DeltaCalculator;
/*     */ import com.zimbra.common.stats.RealtimeStats;
/*     */ import com.zimbra.common.stats.RealtimeStatsCallback;
/*     */ import com.zimbra.common.stats.StatsDumper;
/*     */ import com.zimbra.common.stats.StatsDumperDataSource;
/*     */ import com.zimbra.common.stats.StopWatch;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.util.MemoryStats;
/*     */ import java.io.PrintStream;
/*     */ import java.lang.annotation.Annotation;
/*     */ import java.lang.annotation.Retention;
/*     */ import java.lang.annotation.RetentionPolicy;
/*     */ import java.lang.annotation.Target;
/*     */ import java.lang.management.ManagementFactory;
/*     */ import java.lang.reflect.Field;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.TreeMap;
/*     */ import java.util.concurrent.CopyOnWriteArrayList;
/*     */ import java.util.concurrent.atomic.AtomicInteger;
/*     */ import javax.management.MBeanServer;
/*     */ import javax.management.ObjectName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ZimbraPerf
/*     */ {
/*  68 */   static Log log = LogFactory.getLog(ZimbraPerf.class);
/*     */   
/*     */ 
/*     */   @Description("Number of database connections in use")
/*     */   public static final String RTS_DB_POOL_SIZE = "db_pool_size";
/*     */   
/*     */ 
/*     */   @Description("InnoDB buffer pool hit rate")
/*     */   public static final String RTS_INNODB_BP_HIT_RATE = "innodb_bp_hit_rate";
/*     */   
/*     */ 
/*     */   @Description("Number of LMTP connections")
/*     */   public static final String RTS_LMTP_CONN = "lmtp_conn";
/*     */   
/*     */   @Description("Number of LMTP handler threads")
/*     */   public static final String RTS_LMTP_THREADS = "lmtp_threads";
/*     */   
/*     */   @Description("Number of cleartext POP3 connections")
/*     */   public static final String RTS_POP_CONN = "pop_conn";
/*     */   
/*     */   @Description("Number of POP3 handler threads")
/*     */   public static final String RTS_POP_THREADS = "pop_threads";
/*     */   
/*     */   @Description("Number of SSL POP3 connections")
/*     */   public static final String RTS_POP_SSL_CONN = "pop_ssl_conn";
/*     */   
/*     */   @Description("Number of POP3 SSL handler threads")
/*     */   public static final String RTS_POP_SSL_THREADS = "pop_ssl_threads";
/*     */   
/*     */   @Description("Number of cleartext IMAP connections")
/*     */   public static final String RTS_IMAP_CONN = "imap_conn";
/*     */   
/*     */   @Description("Number of IMAP handler threads")
/*     */   public static final String RTS_IMAP_THREADS = "imap_threads";
/*     */   
/*     */   @Description("Number of SSL IMAP connections")
/*     */   public static final String RTS_IMAP_SSL_CONN = "imap_ssl_conn";
/*     */   
/*     */   @Description("Number of IMAP SSL handler threads")
/*     */   public static final String RTS_IMAP_SSL_THREADS = "imap_ssl_threads";
/*     */   
/*     */   @Description("Number of HTTP handler threads")
/*     */   public static final String RTS_HTTP_THREADS = "http_threads";
/*     */   
/*     */   @Description("Number of idle HTTP handler threads")
/*     */   public static final String RTS_HTTP_IDLE_THREADS = "http_idle_threads";
/*     */   
/*     */   @Description("Number of SOAP sessions")
/*     */   public static final String RTS_SOAP_SESSIONS = "soap_sessions";
/*     */   
/*     */   @Description("Number of mailboxes cached in memory")
/*     */   public static final String RTS_MBOX_CACHE_SIZE = "mbox_cache_size";
/*     */   
/*     */   @Description("Number of message structures cached in memory")
/*     */   public static final String RTS_MSG_CACHE_SIZE = "msg_cache_size";
/*     */   
/*     */   @Description("Number of open file descriptors that reference message content")
/*     */   public static final String RTS_FD_CACHE_SIZE = "fd_cache_size";
/*     */   
/*     */   @Description("File descriptor cache hit rate")
/*     */   public static final String RTS_FD_CACHE_HIT_RATE = "fd_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP ACL cache hit rate")
/*     */   public static final String RTS_ACL_CACHE_HIT_RATE = "acl_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP account cache size")
/*     */   public static final String RTS_ACCOUNT_CACHE_SIZE = "account_cache_size";
/*     */   
/*     */   @Description("LDAP account cache hit rate")
/*     */   public static final String RTS_ACCOUNT_CACHE_HIT_RATE = "account_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP COS cache size")
/*     */   public static final String RTS_COS_CACHE_SIZE = "cos_cache_size";
/*     */   
/*     */   @Description("LDAP COS cache hit rate")
/*     */   public static final String RTS_COS_CACHE_HIT_RATE = "cos_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP domain cache size")
/*     */   public static final String RTS_DOMAIN_CACHE_SIZE = "domain_cache_size";
/*     */   
/*     */   @Description("LDAP domain cache hit rate")
/*     */   public static final String RTS_DOMAIN_CACHE_HIT_RATE = "domain_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP server cache size")
/*     */   public static final String RTS_SERVER_CACHE_SIZE = "server_cache_size";
/*     */   
/*     */   @Description("LDAP server cache hit rate")
/*     */   public static final String RTS_SERVER_CACHE_HIT_RATE = "server_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP UC service cache size")
/*     */   public static final String RTS_UCSERVICE_CACHE_SIZE = "ucservice_cache_size";
/*     */   
/*     */   @Description("LDAP UC service cache hit rate")
/*     */   public static final String RTS_UCSERVICE_CACHE_HIT_RATE = "ucservice_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP zimlet cache size")
/*     */   public static final String RTS_ZIMLET_CACHE_SIZE = "zimlet_cache_size";
/*     */   
/*     */   @Description("LDAP zimlet cache hit rate")
/*     */   public static final String RTS_ZIMLET_CACHE_HIT_RATE = "zimlet_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP group cache size")
/*     */   public static final String RTS_GROUP_CACHE_SIZE = "group_cache_size";
/*     */   
/*     */   @Description("LDAP group cache hit rate")
/*     */   public static final String RTS_GROUP_CACHE_HIT_RATE = "group_cache_hit_rate";
/*     */   
/*     */   @Description("LDAP XMPP cache size")
/*     */   public static final String RTS_XMPP_CACHE_SIZE = "xmpp_cache_size";
/*     */   
/*     */   @Description("LDAP XMPP cache hit rate")
/*     */   public static final String RTS_XMPP_CACHE_HIT_RATE = "xmpp_cache_hit_rate";
/*     */   
/* 181 */   public static final Counter COUNTER_LMTP_RCVD_MSGS = new Counter();
/* 182 */   public static final Counter COUNTER_LMTP_RCVD_BYTES = new Counter();
/* 183 */   public static final Counter COUNTER_LMTP_RCVD_RCPT = new Counter();
/* 184 */   public static final Counter COUNTER_LMTP_DLVD_MSGS = new Counter();
/* 185 */   public static final Counter COUNTER_LMTP_DLVD_BYTES = new Counter();
/* 186 */   public static final StopWatch STOPWATCH_DB_CONN = new StopWatch();
/* 187 */   public static final StopWatch STOPWATCH_LDAP_DC = new StopWatch();
/* 188 */   public static final StopWatch STOPWATCH_MBOX_ADD_MSG = new StopWatch();
/* 189 */   public static final StopWatch STOPWATCH_MBOX_GET = new StopWatch();
/* 190 */   public static final Counter COUNTER_MBOX_CACHE = new Counter();
/* 191 */   public static final Counter COUNTER_MBOX_MSG_CACHE = new Counter();
/* 192 */   public static final Counter COUNTER_MBOX_ITEM_CACHE = new Counter();
/* 193 */   public static final StopWatch STOPWATCH_SOAP = new StopWatch();
/* 194 */   public static final StopWatch STOPWATCH_IMAP = new StopWatch();
/* 195 */   public static final StopWatch STOPWATCH_POP = new StopWatch();
/* 196 */   public static final Counter COUNTER_IDX_WRT = new Counter();
/* 197 */   public static final Counter COUNTER_IDX_WRT_OPENED = new Counter();
/* 198 */   public static final Counter COUNTER_IDX_WRT_OPENED_CACHE_HIT = new Counter();
/* 199 */   public static final Counter COUNTER_CALENDAR_CACHE_HIT = new Counter();
/* 200 */   public static final Counter COUNTER_CALENDAR_CACHE_MEM_HIT = new Counter();
/* 201 */   public static final Counter COUNTER_CALENDAR_CACHE_LRU_SIZE = new Counter();
/* 202 */   public static final Counter COUNTER_IDX_BYTES_WRITTEN = new Counter();
/* 203 */   public static final Counter COUNTER_IDX_BYTES_READ = new Counter();
/* 204 */   public static final Counter COUNTER_BLOB_INPUT_STREAM_READ = new Counter();
/* 205 */   public static final Counter COUNTER_BLOB_INPUT_STREAM_SEEK_RATE = new Counter();
/* 206 */   public static final StopWatch STOPWATCH_EWS = new StopWatch();
/*     */   
/* 208 */   public static final ActivityTracker SOAP_TRACKER = new ActivityTracker("soap.csv");
/* 209 */   public static final ActivityTracker IMAP_TRACKER = new ActivityTracker("imap.csv");
/* 210 */   public static final ActivityTracker POP_TRACKER = new ActivityTracker("pop3.csv");
/* 211 */   public static final ActivityTracker LDAP_TRACKER = new ActivityTracker("ldap.csv");
/* 212 */   public static final ActivityTracker SYNC_TRACKER = new ActivityTracker("sync.csv");
/* 213 */   public static final ActivityTracker SQL_TRACKER = new ActivityTracker("sql.csv");
/*     */   
/*     */   private static int mailboxCacheSize;
/* 216 */   private static long mailboxCacheSizeTimestamp = 0L;
/*     */   private static JmxServerStats jmxServerStats;
/* 218 */   private static Map<String, String> descriptions = new TreeMap(String.CASE_INSENSITIVE_ORDER);
/*     */   
/*     */   public static String getDescription(String statName) {
/* 221 */     return (String)descriptions.get(statName);
/*     */   }
/*     */   
/* 224 */   private static RealtimeStats realtimeStats = new RealtimeStats(new String[] { "db_pool_size", "innodb_bp_hit_rate", "lmtp_conn", "lmtp_threads", "pop_conn", "pop_threads", "pop_ssl_conn", "pop_ssl_threads", "imap_conn", "imap_threads", "imap_ssl_conn", "imap_ssl_threads", "http_idle_threads", "http_threads", "soap_sessions", "mbox_cache_size", "msg_cache_size", "fd_cache_size", "fd_cache_hit_rate", "acl_cache_hit_rate", "account_cache_size", "account_cache_hit_rate", "cos_cache_size", "cos_cache_hit_rate", "domain_cache_size", "domain_cache_hit_rate", "server_cache_size", "server_cache_hit_rate", "ucservice_cache_size", "ucservice_cache_hit_rate", "zimlet_cache_size", "zimlet_cache_hit_rate", "group_cache_size", "group_cache_hit_rate", "xmpp_cache_size", "xmpp_cache_hit_rate" });
/*     */   
/*     */ 
/*     */   @Description("Number of messages received over LMTP")
/*     */   private static final String DC_LMTP_RCVD_MSGS = "lmtp_rcvd_msgs";
/*     */   
/*     */ 
/*     */   @Description("Number of bytes received over LMTP")
/*     */   private static final String DC_LMTP_RCVD_BYTES = "lmtp_rcvd_bytes";
/*     */   
/*     */ 
/*     */   @Description("Number of LMTP recipients")
/*     */   private static final String DC_LMTP_RCVD_RCPT = "lmtp_rcvd_rcpt";
/*     */   
/*     */ 
/*     */   @Description("Number of messages delivered to mailboxes as a result of LMTP delivery")
/*     */   private static final String DC_LMTP_DLVD_MSGS = "lmtp_dlvd_msgs";
/*     */   
/*     */ 
/*     */   @Description("Number of bytes of data delivered to mailboxes as a result of LMTP delivery")
/*     */   private static final String DC_LMTP_DLVD_BYTES = "lmtp_dlvd_bytes";
/*     */   
/*     */ 
/*     */   @Description("Number of times that the server got a database connection from the pool")
/*     */   private static final String DC_DB_CONN_COUNT = "db_conn_count";
/*     */   
/*     */ 
/*     */   @Description("Average latency (ms) of getting a database connection from the pool")
/*     */   private static final String DC_DB_CONN_MS_AVG = "db_conn_ms_avg";
/*     */   
/*     */ 
/*     */   @Description("Number of times that the server got an LDAP directory context")
/*     */   private static final String DC_LDAP_DC_COUNT = "ldap_dc_count";
/*     */   
/*     */ 
/*     */   @Description("Average latency (ms) of getting an LDAP directory context")
/*     */   private static final String DC_LDAP_DC_MS_AVG = "ldap_dc_ms_avg";
/*     */   
/*     */ 
/*     */   @Description("Number of messages that were added to a mailbox")
/*     */   private static final String DC_MBOX_ADD_MSG_COUNT = "mbox_add_msg_count";
/*     */   
/*     */ 
/*     */   @Description("Average latency (ms) of adding a message to a mailbox")
/*     */   private static final String DC_MBOX_ADD_MSG_MS_AVG = "mbox_add_msg_ms_avg";
/*     */   
/*     */ 
/*     */   @Description("Number of times that the server got a mailbox from the cache")
/*     */   private static final String DC_MBOX_GET_COUNT = "mbox_get_count";
/*     */   
/*     */ 
/*     */   @Description("Average latency (ms) of getting a mailbox from the cache")
/*     */   private static final String DC_MBOX_GET_MS_AVG = "mbox_get_ms_avg";
/*     */   
/*     */ 
/*     */   @Description("Mailbox cache hit rate")
/*     */   private static final String DC_MBOX_CACHE = "mbox_cache";
/*     */   
/*     */ 
/*     */   @Description("Message cache hit rate")
/*     */   private static final String DC_MBOX_MSG_CACHE = "mbox_msg_cache";
/*     */   
/*     */ 
/*     */   @Description("Item cache hit rate")
/*     */   private static final String DC_MBOX_ITEM_CACHE = "mbox_item_cache";
/*     */   
/*     */ 
/*     */   @Description("Number of SOAP requests received")
/*     */   private static final String DC_SOAP_COUNT = "soap_count";
/*     */   
/*     */ 
/*     */   @Description("Average processing time (ms) of SOAP requests")
/*     */   private static final String DC_SOAP_MS_AVG = "soap_ms_avg";
/*     */   
/*     */ 
/*     */   @Description("Number of IMAP requests received")
/*     */   private static final String DC_IMAP_COUNT = "imap_count";
/*     */   
/*     */   @Description("Average processing time (ms) of IMAP requests")
/*     */   private static final String DC_IMAP_MS_AVG = "imap_ms_avg";
/*     */   
/*     */   @Description("Number of POP3 requests received")
/*     */   private static final String DC_POP_COUNT = "pop_count";
/*     */   
/*     */   @Description("Average processing time (ms) of POP3 requests")
/*     */   private static final String DC_POP_MS_AVG = "pop_ms_avg";
/*     */   
/*     */   @Description("Number of times that the file descriptor cache read message data from disk")
/*     */   private static final String DC_BIS_READ = "bis_read";
/*     */   
/*     */   @Description("Percentage of file descriptor cache disk reads that required a seek")
/*     */   private static final String DC_BIS_SEEK_RATE = "bis_seek_rate";
/*     */   
/*     */   @Description("Average number of concurrent index writers")
/*     */   private static final String DC_IDX_WRT_AVG = "idx_wrt_avg";
/*     */   
/*     */   @Description("Accumulated number of index writers opened")
/*     */   private static final String DC_IDX_WRT_OPENED = "idx_wrt_opened";
/*     */   
/*     */   @Description("Accumulated number of cache hits when opening an index writer")
/*     */   private static final String DC_IDX_WRT_OPENED_CACHE_HIT = "idx_wrt_opened_cache_hit";
/*     */   
/*     */   @Description("Accumulated bytes written by Lucene")
/*     */   private static final String DC_IDX_BYTES_WRITTEN = "idx_bytes_written";
/*     */   
/*     */   @Description("Average of idx_bytes_written")
/*     */   private static final String DC_IDX_BYTES_WRITTTEN_AVG = "idx_bytes_written_avg";
/*     */   
/*     */   @Description("Accumulated bytes read by Lucene")
/*     */   private static final String DC_IDX_BYTES_READ = "idx_bytes_read";
/*     */   
/*     */   @Description("Average of idx_bytes_read")
/*     */   private static final String DC_IDX_BYTES_READ_AVG = "idx_bytes_read_avg";
/*     */   
/*     */   @Description("Hit rate of calendar summary cache, counting cache hit from both memory and file")
/*     */   private static final String DC_CALCACHE_HIT = "calcache_hit";
/*     */   
/*     */   @Description("Hit rate of calendar summary cache, counting cache hit from memory only")
/*     */   private static final String DC_CALCACHE_MEM_HIT = "calcache_mem_hit";
/*     */   
/*     */   @Description("Number of calendars (folders) in the calendar summary cache LRU in Java heap")
/*     */   private static final String DC_CALCACHE_LRU_SIZE = "calcache_lru_size";
/*     */   
/* 347 */   private static CopyOnWriteArrayList<Accumulator> sAccumulators = new CopyOnWriteArrayList(new Accumulator[] { new DeltaCalculator(COUNTER_LMTP_RCVD_MSGS).setTotalName("lmtp_rcvd_msgs"), new DeltaCalculator(COUNTER_LMTP_RCVD_BYTES).setTotalName("lmtp_rcvd_bytes"), new DeltaCalculator(COUNTER_LMTP_RCVD_RCPT).setTotalName("lmtp_rcvd_rcpt"), new DeltaCalculator(COUNTER_LMTP_DLVD_MSGS).setTotalName("lmtp_dlvd_msgs"), new DeltaCalculator(COUNTER_LMTP_DLVD_BYTES).setTotalName("lmtp_dlvd_bytes"), new DeltaCalculator(STOPWATCH_DB_CONN).setCountName("db_conn_count").setAverageName("db_conn_ms_avg"), new DeltaCalculator(STOPWATCH_LDAP_DC).setCountName("ldap_dc_count").setAverageName("ldap_dc_ms_avg"), new DeltaCalculator(STOPWATCH_MBOX_ADD_MSG).setCountName("mbox_add_msg_count").setAverageName("mbox_add_msg_ms_avg"), new DeltaCalculator(STOPWATCH_MBOX_GET).setCountName("mbox_get_count").setAverageName("mbox_get_ms_avg"), new DeltaCalculator(COUNTER_MBOX_CACHE).setAverageName("mbox_cache"), new DeltaCalculator(COUNTER_MBOX_MSG_CACHE).setAverageName("mbox_msg_cache"), new DeltaCalculator(COUNTER_MBOX_ITEM_CACHE).setAverageName("mbox_item_cache"), new DeltaCalculator(STOPWATCH_SOAP).setCountName("soap_count").setAverageName("soap_ms_avg"), new DeltaCalculator(STOPWATCH_IMAP).setCountName("imap_count").setAverageName("imap_ms_avg"), new DeltaCalculator(STOPWATCH_POP).setCountName("pop_count").setAverageName("pop_ms_avg"), new DeltaCalculator(COUNTER_IDX_WRT).setAverageName("idx_wrt_avg"), new DeltaCalculator(COUNTER_IDX_WRT_OPENED).setTotalName("idx_wrt_opened"), new DeltaCalculator(COUNTER_IDX_WRT_OPENED_CACHE_HIT).setTotalName("idx_wrt_opened_cache_hit"), new DeltaCalculator(COUNTER_CALENDAR_CACHE_HIT).setAverageName("calcache_hit"), new DeltaCalculator(COUNTER_CALENDAR_CACHE_MEM_HIT).setAverageName("calcache_mem_hit"), new DeltaCalculator(COUNTER_CALENDAR_CACHE_LRU_SIZE).setAverageName("calcache_lru_size"), new DeltaCalculator(COUNTER_IDX_BYTES_WRITTEN).setTotalName("idx_bytes_written").setAverageName("idx_bytes_written_avg"), new DeltaCalculator(COUNTER_IDX_BYTES_READ).setTotalName("idx_bytes_read").setAverageName("idx_bytes_read_avg"), new DeltaCalculator(COUNTER_BLOB_INPUT_STREAM_READ).setTotalName("bis_read"), new DeltaCalculator(COUNTER_BLOB_INPUT_STREAM_SEEK_RATE).setAverageName("bis_seek_rate"), realtimeStats });
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static void initDescriptions()
/*     */   {
/* 380 */     descriptions = Collections.synchronizedMap(descriptions);
/*     */     
/* 382 */     for (Field f : ZimbraPerf.class.getDeclaredFields()) {
/* 383 */       if (f.isAnnotationPresent(Description.class)) {
/*     */         try {
/* 385 */           Object o = f.get(null);
/* 386 */           if ((o instanceof String)) {
/* 387 */             String description = ((Description)f.getAnnotation(Description.class)).value();
/* 388 */             descriptions.put((String)o, description);
/*     */           }
/*     */         } catch (IllegalAccessException e) {
/* 391 */           ZimbraLog.perf.warn("Unexpected @Description annotation on field %s.", f.getName(), e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static Map<String, Object> getStats()
/*     */   {
/* 401 */     Map<String, Object> stats = new LinkedHashMap();
/*     */     
/* 403 */     List<Accumulator> accumulators = new ArrayList();
/* 404 */     accumulators.addAll(sAccumulators);
/* 405 */     accumulators.add(realtimeStats);
/*     */     
/* 407 */     for (Accumulator a : accumulators) {
/* 408 */       List<String> names = a.getNames();
/* 409 */       List<Object> data = a.getData();
/* 410 */       for (int i = 0; i < names.size(); i++) {
/* 411 */         stats.put(names.get(i), data.get(i));
/*     */       }
/*     */     }
/*     */     
/* 415 */     return stats;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void addRealtimeStatName(String name, String description)
/*     */   {
/* 424 */     if (sIsInitialized)
/* 425 */       throw new IllegalStateException("Cannot add stat name after ZimbraPerf.initialize() is called");
/* 426 */     ZimbraLog.perf.debug("Adding realtime stat '%s': %s", new Object[] { name, description });
/* 427 */     realtimeStats.addName(name);
/* 428 */     descriptions.put(name, description);
/*     */   }
/*     */   
/*     */   public static JmxServerStatsMBean getMonitoringStats() {
/* 432 */     return jmxServerStats;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 439 */   private static AtomicInteger sPrepareCount = new AtomicInteger(0);
/*     */   private static final long CSV_DUMP_FREQUENCY = 60000L;
/*     */   
/* 442 */   public static int getPrepareCount() { return sPrepareCount.get(); }
/*     */   
/*     */   public static void incrementPrepareCount()
/*     */   {
/* 446 */     sPrepareCount.getAndIncrement();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void addStatsCallback(RealtimeStatsCallback callback)
/*     */   {
/* 454 */     realtimeStats.addCallback(callback);
/*     */   }
/*     */   
/*     */ 
/* 458 */   private static boolean sIsInitialized = false;
/*     */   
/*     */   public static synchronized void initialize() {
/* 461 */     if (sIsInitialized) {
/* 462 */       log.warn("Detected a second call to ZimbraPerf.initialize()", new Exception());
/* 463 */       return;
/*     */     }
/* 465 */     initDescriptions();
/*     */     
/* 467 */     addStatsCallback(new ServerStatsCallback());
/* 468 */     addStatsCallback(new JettyStats());
/*     */     
/* 470 */     StatsDumper.schedule(new MailboxdStats(null), 60000L);
/*     */     
/* 472 */     StatsDumper.schedule(SOAP_TRACKER, 60000L);
/* 473 */     StatsDumper.schedule(IMAP_TRACKER, 60000L);
/* 474 */     StatsDumper.schedule(POP_TRACKER, 60000L);
/* 475 */     StatsDumper.schedule(LDAP_TRACKER, 60000L);
/* 476 */     StatsDumper.schedule(SYNC_TRACKER, 60000L);
/* 477 */     StatsDumper.schedule(SQL_TRACKER, 60000L);
/*     */     
/* 479 */     ThreadStats threadStats = new ThreadStats("threads.csv");
/* 480 */     StatsDumper.schedule(threadStats, 60000L);
/*     */     
/*     */ 
/* 483 */     MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
/* 484 */     jmxServerStats = new JmxServerStats();
/*     */     try {
/* 486 */       jmxServer.registerMBean(jmxServerStats, new ObjectName("ZimbraCollaborationSuite:type=ServerStats"));
/*     */     } catch (Exception e) {
/* 488 */       ZimbraLog.perf.warn("Unable to register JMX interface.", e);
/*     */     }
/*     */     
/* 491 */     sIsInitialized = true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   static int getMailboxCacheSize()
/*     */   {
/* 499 */     long now = System.currentTimeMillis();
/* 500 */     if (now - mailboxCacheSizeTimestamp > 60000L) {
/*     */       try {
/* 502 */         mailboxCacheSize = MailboxManager.getInstance().getCacheSize();
/*     */       } catch (ServiceException e) {
/* 504 */         ZimbraLog.perf.warn("Unable to determine mailbox cache size.", e);
/*     */       }
/* 506 */       mailboxCacheSizeTimestamp = now;
/*     */     }
/* 508 */     return mailboxCacheSize;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final class MailboxdStats
/*     */     implements StatsDumperDataSource
/*     */   {
/*     */     public String getFilename()
/*     */     {
/* 520 */       return "mailboxd.csv";
/*     */     }
/*     */     
/*     */     public String getHeader()
/*     */     {
/* 525 */       List<String> columns = new ArrayList();
/* 526 */       for (Accumulator a : ZimbraPerf.sAccumulators) {
/* 527 */         for (String column : a.getNames()) {
/* 528 */           columns.add(column);
/*     */         }
/*     */       }
/* 531 */       return StringUtil.join(",", columns);
/*     */     }
/*     */     
/*     */     public Collection<String> getDataLines()
/*     */     {
/* 536 */       List<Object> data = new ArrayList();
/* 537 */       for (Accumulator a : ZimbraPerf.sAccumulators) {
/* 538 */         synchronized (a) {
/* 539 */           data.addAll(a.getData());
/* 540 */           a.reset();
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 545 */       for (int i = 0; i < data.size(); i++) {
/* 546 */         if (data.get(i) == null) {
/* 547 */           data.set(i, "");
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 552 */       String line = StringUtil.join(",", data);
/* 553 */       List<String> retVal = new ArrayList(1);
/* 554 */       retVal.add(line);
/*     */       
/*     */ 
/* 557 */       ZimbraPerf.jmxServerStats.reset();
/*     */       
/* 559 */       return retVal;
/*     */     }
/*     */     
/*     */     public boolean hasTimestampColumn()
/*     */     {
/* 564 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 569 */     initDescriptions();
/* 570 */     MemoryStats.startup();
/* 571 */     for (String field : descriptions.keySet()) {
/* 572 */       System.out.println(field + ": " + (String)descriptions.get(field));
/*     */     }
/*     */   }
/*     */   
/*     */   @Target({java.lang.annotation.ElementType.FIELD})
/*     */   @Retention(RetentionPolicy.RUNTIME)
/*     */   private static @interface Description
/*     */   {
/*     */     String value();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/stats/ZimbraPerf.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */